package com.yanqu.road.server.cmd.cricket.court;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.cricket.CricketModule;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_CRICKET_MATCH, desc = "匹配战斗")
public class CricketMatchCmd extends AbstractCommand {

    private boolean isRefresh;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CricketProto.CricketMatchReqMsg reqMsg = CricketProto.CricketMatchReqMsg.parseFrom(packet.getBytes());
        isRefresh = reqMsg.getIsRefresh();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        CricketModule cricketModule = player.getModule(CricketModule.class);
        ret = cricketModule.preMatch(isRefresh);
        if (ret != 0) {
            return;
        }
        // 上跨服
        CricketProto.CrossCricketMatchReqMsg.Builder reqMsg = CricketProto.CrossCricketMatchReqMsg.newBuilder();
        reqMsg.setIsRefresh(isRefresh);
        player.sendPacket(Protocol.C_CRICKET_MATCH, reqMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            CricketProto.CricketMatchRespMsg.Builder respMsg = CricketProto.CricketMatchRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_CRICKET_MATCH, respMsg);
        }
    }
}
